package com.hyc.myvideo.ui.fragment.home;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.adapter.HomeClassAdapter;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.HomeCourse;
import com.hyc.myvideo.ui.activity.home.AudioActivity;
import com.hyc.myvideo.ui.activity.home.CourseActivity;
import com.hyc.myvideo.ui.activity.home.HomeDetailActivity;
import com.hyc.myvideo.ui.activity.home.TrainingCatalogActivity;
import com.hyc.myvideo.ui.activity.home.VideoActivity;
import com.hyc.myvideo.ui.base.BaseFragment;
import com.hyc.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 免费推荐课程
 * Created by dfeads on 2017/7/14.
 */

public class FreeCourseFragment extends BaseFragment {
  @BindView(R.id.fragment_teacher_pull_refresh) PullToRefreshListView mPullRefresh;
  private int page = 1;

  private HomeClassAdapter homeClassAdapter;
  private List<HomeCourse> homeCourseList;
  private String curriculum_class_id = "";
  private boolean isRefresh = true;

  @Override protected int getLayoutId() {
    return R.layout.fragment_teacher;
  }

  @Override protected void initView() {
    CourseActivity courseActivity = (CourseActivity) getActivity();
    curriculum_class_id = courseActivity.getCurriculum_class_id();
    if (!StringUtils.isEmpty(curriculum_class_id)) {
      // 0=免费，1=vip,2=收费
      api.getHomeClassList(curriculum_class_id, page, "0");
    }
  }

  @Override protected void initData() {
    homeCourseList = new ArrayList<>();
    homeClassAdapter = new HomeClassAdapter(getContext(), homeCourseList);
    mPullRefresh.setAdapter(homeClassAdapter);
    mPullRefresh.setMode(PullToRefreshBase.Mode.BOTH);
    mPullRefresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
      @Override public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        isRefresh = true;
        page = 1;
        api.getHomeClassList(curriculum_class_id, page, "0");
      }

      @Override public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        isRefresh = false;
        page++;
        api.getHomeClassList(curriculum_class_id, page, "0");
      }
    });

    mPullRefresh.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        HomeCourse homeCourse = homeCourseList.get(position - 1);
        //1系列 2单个
        if (homeCourse.getState() == 1) {
          startActivity(new Intent(getContext(), TrainingCatalogActivity.class).putExtra("id",
              homeCourse.getId()));
        } else {
          startAct(homeCourse);
        }
      }
    });
  }

  /**
   * list的跳转
   */
  private void startAct(HomeCourse hCourse) {
    //1=视频,2=音频,3=图文
    switch (hCourse.getCurriculum_hour_type()) {
      case 1:
        startActivity(
            new Intent(getContext(), VideoActivity.class).putExtra("id", hCourse.getId()));
        break;
      case 2:
        startActivity(
            new Intent(getContext(), AudioActivity.class).putExtra("id", hCourse.getId()));
        break;
      case 3:
        startActivity(
            new Intent(getActivity(), HomeDetailActivity.class).putExtra("id", hCourse.getId()));
        break;
    }
  }

  @Override protected void onNexts(String result, String method) {
    BaseResult<List<HomeCourse>> homeCourses =
        JSONObject.parseObject(result, new TypeReference<BaseResult<List<HomeCourse>>>() {
        });
    if (homeCourses.isCode()) {
      if (isRefresh) {
        homeCourseList.clear();
      }
      homeCourseList.addAll(homeCourses.getResult());
      homeClassAdapter.updateListView(homeCourseList);
    } else {
      Toaster.show(homeCourses.getMsg());
    }
    if (mPullRefresh.isRefreshing()) {
      mPullRefresh.onRefreshComplete();
    }
  }

  @Override protected void onError() {
    if (mPullRefresh.isRefreshing()) {
      mPullRefresh.onRefreshComplete();
    }
  }
}
